<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/basic';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();
	
	public function init()
	{
		Yii::$classMap = array_merge( Yii::$classMap, array(
            'CaptchaExtendedAction' => Yii::getPathOfAlias('ext.captchaExtended').DIRECTORY_SEPARATOR.'CaptchaExtendedAction.php',
            'CaptchaExtendedValidator' => Yii::getPathOfAlias('ext.captchaExtended').DIRECTORY_SEPARATOR.'CaptchaExtendedValidator.php'
        ));
		parent::init();
	}
	
	public function filters()
	{
		return array(
			'AccessControl' // make filterAccessControl method come into force
		);
	}
	
	public function filterAccessControl($filterChain)
	{
        $filter=new AccessControlFilter;  // CHANGED THIS
        $filter->setRules($this->accessRules());
        $filter->filter($filterChain);
	}
	
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModelByPk($id, $modelClass)
	{
		$class = CActiveRecord::model($modelClass);
		 
		$model = $class->findByPk($id);
		
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	
	public function loadModelByAttributes($attributes, $modelClass)
	{
		$class = CActiveRecord::model($modelClass);
		 
		$model = $class->findByAttributes($attributes);
		
		if($model === null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	
	protected function _returnAjax($msg = '', $byJson = true)
	{
		if($byJson){
			$msg = json_encode($msg);
		}
		echo $msg;
		$this->_end();
	}
    
	protected function _end($status=0, $exit=true)
	{
		if($exit)
			exit($status);
	}
	
	protected function FormAjaxValidation($formModel)
	{
	    if(isset($_POST['ajax']))
	    {
	        echo CActiveForm::validate($formModel);
	        Yii::app()->end();
	    }
	}
}